package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.*;
import com.example.common.util.R;
import com.example.buildtable.service.ColumnMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "栏目菜单")
@RequestMapping(value = "/columnMenu/")
public class ColumnMenuController {
    @Autowired
    private ColumnMenuService columnMenuService;
    @Operation(summary = "查询所有栏目菜单数据")
    @GetMapping(value = "/getAllColumnMenu")
    public R getAllColumnMenu() {
        LambdaJoinQueryWrapper<ColumnMenu> wrapper = new LambdaJoinQueryWrapper<>(ColumnMenu.class);
        wrapper.innerJoin(OnelevelMenu.class, OnelevelMenu::getOnelevelMenuId, ColumnMenu::getOnelevelMenuId)
                .innerJoin(TwolevelMenu.class, TwolevelMenu::getTwolevelMenuId, ColumnMenu::getTwolevelMenuId);
        List<ColumnMenu> columnMenuList = columnMenuService.list(wrapper);
        return R.ok("查询所有栏目菜单数据成功").data("columnMenuList", columnMenuList);
    }
    
    @Operation(summary = "增加栏目菜单")
    @PostMapping(value = "/addColumnMenu")
    public R addColumnMenu(ColumnMenu columnMenu) {
        boolean flag = columnMenuService.save(columnMenu);
        if (flag) {
            return R.ok("栏目菜单增加成功");
        } else {
            return R.ok("栏目菜单增加失败");
        }
    }

    @Operation(summary = "删除栏目菜单")
    @GetMapping(value = "/deleteColumnMenu/{columnMenuId}")
    public R deleteColumnMenu(@PathVariable("columnMenuId") Integer columnMenuId) {
        boolean flag = columnMenuService.removeById(columnMenuId);
        if (flag) {
            return R.ok("栏目菜单删除成功");
        } else {
            return R.ok("栏目菜单删除失败");
        }
    }

    @Operation(summary = "按照栏目菜单查看")
    @GetMapping(value = "/getColumnMenuById/{columnMenuId}")
    public R getColumnMenuById(@PathVariable("columnMenuId") Integer columnMenuId) {
        LambdaJoinQueryWrapper<ColumnMenu> wrapper = new LambdaJoinQueryWrapper<>(ColumnMenu.class);
        wrapper.innerJoin(OnelevelMenu.class, OnelevelMenu::getOnelevelMenuId, ColumnMenu::getOnelevelMenuId)
                .innerJoin(TwolevelMenu.class, TwolevelMenu::getTwolevelMenuId, ColumnMenu::getTwolevelMenuId);
        wrapper.eq(ColumnMenu::getColumnMenuId, columnMenuId);
        ColumnMenu columnMenu = columnMenuService.getOne(wrapper);
        if (columnMenu != null) {
            return R.ok("按照栏目菜单查看成功").data("columnMenu", columnMenu);
        } else {
            return R.error("按照栏目菜单查看失败");
        }
    }

    @Operation(summary = "修改栏目菜单")
    @PostMapping(value = "/updateColumnMenu")
    public R updateColumnMenu(ColumnMenu columnMenu) {
        boolean flag = columnMenuService.updateById(columnMenu);
        if (flag) {
            return R.ok("栏目菜单修改成功");
        } else {
            return R.ok("栏目菜单修改失败");
        }
    }
}